{/* LAYOUT模板 */}
	<layout name="layout"/>
{/* LAYOUT模板 */}
{/* import Js,Css */}
	<js href="__PUBLIC__/js/admin/listTable.js"/>
	<js href="__PUBLIC__/js/admin/js.util.js"/>
	<css href="__PUBLIC__/css/admin/common.css"/>
	<js href="__PUBLIC__/js/admin/js.selectzone.js"/>
	
	<css href="__PUBLIC__/css/jquery-ui.css" />
	<js href="__PUBLIC__/js/core/jquery-ui.1.10.4.js" />
	<js href="__PUBLIC__/js/core/plugins.js"/>
{/* import js,css */}
<!-- [position] -->
	<include file="Template:position"/>
<!-- [position] -->


<div class="main-div">
	<form method="post" action="__URL__/{$action}" name="theForm" enctype="multipart/form-data" onSubmit="return validate()">
		<table cellspacing="1" cellpadding="3" width="100%">
			<tr>
				<td class="label" align="right">优惠活动名称：</td>
				<td><input name="act_name" type="text" id="act_name" value="{$favourable.act_name}" size="40" /></td>
			</tr>
			<tr>
				<td class="label" align="right">优惠开始时间：</td>
				<td><input name="start_time" type="text" id="start_time" value="{$favourable.start_time}" readonly="readonly" />
			</td>
			</tr>
			<tr>
				<td class="label" align="right">优惠结束时间：</td>
				<td><input name="end_time" type="text" id="end_time" value="{$favourable.end_time}" readonly="readonly" />
			</td>
			</tr>
			<tr>
				<td class="label" align="right">享受优惠的会员等级：</td>
				<td>
					<volist name="user_rank_list" id="user_rank">
						<input type="checkbox" name="user_rank[]" value="{$user_rank.rank_id}" <if condition="$user_rank['checked']">checked="true"</if> />
							{$user_rank.rank_name} 
					</volist>
				</td>
			</tr>
			<tr>
				<td class="label" align="right">优惠范围：</td>
				<td>
					<select name="act_range" onchange="changeRange(this.value)">
						<option value="0" selected="selected" <if condition="$favourable['act_range'] eq 0">selected="selected"</if>>{$Think.lang.far_all}</option>
						<option value="1" <if condition="$favourable['act_range'] eq 1">selected="selected"</if> >{$Think.lang.far_category}</option>
						<option value="2" <if condition="$favourable['act_range'] eq 2">selected="selected"</if> >{$Think.lang.far_brand}</option>
						<option value="3" <if condition="$favourable['act_range'] eq 3">selected="selected"</if> >{$Think.lang.far_goods}</option>
					</select>
					<div id="range-div">
						<volist name="act_range_ext" id="item">
							<input name="act_range_ext[]" type="checkbox" value="{$item.id}" checked="checked" />{$item.name} 
						</volist>
					</div>
				</td>
			</tr>
			<tr id="range_search" <if condition="$favourable['act_range'] eq 0"> style="display:none"</if> >
				<td align="right">搜索并加入优惠范围</td>
				<td>
					<input name="keyword" type="text" id="keyword">
					<input name="search" type="button" id="search" value="搜索"  onclick="searchItem()" />
					<select name="result" id="result">
					</select> 
					<input type="button" name="add_range" value="+"  onclick="addRange()" />
				</td>
			</tr>
			<tr>
				<td class="label" align="right">金额下限：</td>
				<td><input name="min_amount" type="text" id="min_amount" value="{$favourable.min_amount}"></td>
			</tr>
			<tr>
				<td class="label" align="right">金额上限：</td>
				<td>
					<input name="max_amount" type="text" id="max_amount" value="{$favourable.max_amount}">
					0表示没有上限
				</td>
			</tr>
			<tr>
				<td class="label" align="right">
					<a href="javascript:showNotice('NoticeActType');" title="{$lang.form_notice}">
					<img src="__PUBLIC__/image/admin/notice.gif" width="16" height="16" border="0" alt="{$lang.form_notice}"></a>优惠方式：
				</td>
				<td>
					<select name="act_type" id="act_type" onchange="changeType(this.value)">
						<option value="0" <if condition="$favourable['act_type'] eq 0">selected="selected"</if> >{$Think.lang.fat_goods}</option>
						<option value="1" <if condition="$favourable['act_type'] eq 1">selected="selected"</if> >{$Think.lang.fat_price}</option>
						<option value="2" <if condition="$favourable['act_type'] eq 2">selected="selected"</if> >{$Think.lang.fat_discount}</option>
					</select>
					<input name="act_type_ext" type="text" id="act_type_ext" value="{$favourable.act_type_ext}" size="10" />
					<br/>
					<span class="notice-span" <if condition="$help_open">style="display:block"<else/> style="display:none"</if> id="NoticeActType">
						{$Think.lang.notice_act_type}
					</span>
					<div id="gift-div" style="width:60%">
						<table id="gift-table">
							<if condition="$favourable['gift']">
								<tr align="center">
									<td><strong>{$Think.lang.js_languages.gift}</strong></td>
									<td><strong>{$Think.lang.js_languages.price}</strong></td>
								</tr>
								<volist name="favourable.gift" id="goods">
								<tr>
									<td><input type="checkbox" name="gift_id[{$key}]" value="{$goods.id}" checked="checked" />{$goods.name}</td>
									<td align="right">
										<input name="gift_price[{$key}]" type="text" value="{$goods.price}" size="10" style="text-align:right" />
										<input name="gift_name[{$key}]" type="hidden" value="{$goods.name}" />
									</td>
								</tr>
								</volist>
							</if>
						</table>
					</div>
				</td>
			</tr>
			<tr id="type_search" <if condition="$favourable['act_type'] eq 0">style="display:none" </if> >
				<td align="right">{$Think.lang.label_search_and_add_gift}</td>
				<td>
					<input name="keyword1" type="text" id="keyword1" />
					<input name="search1" type="button" id="search1" value="搜索"  onclick="searchItem1()" />
					<select name="result1" id="result1">
					</select>
					<input name="add_gift" type="button"  id="add_gift" onclick="addGift()" value="+" />
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="submit"  value="确定" />
				<input type="reset"  value="重置" />
				<input type="hidden" name="act" value="{$form_action}" />
				<input type="hidden" name="id" value="{$favourable.act_id}" />    </td>
			</tr>
		</table>
	</form>
</div>


<script language="JavaScript">
<!--
onload = function()
{
    // 开始检查订单
    //startCheckOrder();
//    changeRange(document.forms['theForm'].elements['act_range'].value);
//    changeType(document.forms['theForm'].elements['act_type'].value);
}
/**
 * 检查表单输入的数据
 */
function validate()
{
    validator = new Validator("theForm");
    validator.required('act_name', act_name_not_null);
    validator.isNumber('min_amount', min_amount_not_number, true);
    validator.isNumber('max_amount', max_amount_not_number, true);
    validator.isNumber('act_type_ext', act_type_ext_not_number, true);
    validator.islt('start_time', 'end_time', start_lt_end);
    if (document.forms['theForm'].elements['max_amount'].value > 0)
    {
      validator.gt('max_amount', 'min_amount', amount_invalid);
    }

    return validator.passed();
}

function searchItem(){
	var filter = new Object;
	filter.keyword  = document.forms['theForm'].elements['keyword'].value;
	filter.act_range = document.forms['theForm'].elements['act_range'].value;
	if (filter.act_range == 0){
		alert(all_need_not_search);
		return;
	}
	
	//Ajax.call('favourable.php?is_ajax=1&act=search', filter, searchResponse, 'GET', 'jsON');
	var options	=	{
		url:'__URL__/search',
		type:'get',
		dataType:'json',
		data:{'jsON':filter},
		beforeSend:function(){},
		success:function(result){
			searchResponse(result);
		}
	}
	$.ajax(options);
}

function searchResponse(result)
{
  if (result.error == '1' && result.message != '')
  {
    alert(result.message);
	return;
  }

  var sel = document.forms['theForm'].elements['result'];

  sel.length = 0;

  /* 创建 options */
  var goods = result.content;
  if (goods)
  {
    for (i = 0; i < goods.length; i++)
    {
      var opt = document.createElement("OPTION");
      opt.value = goods[i].id;
      opt.text  = goods[i].name;
      sel.options.add(opt);
    }
  }

  return;
}

/**
 * 改变优惠范围
 * @param int rangeId
 */
function changeRange(rangeId)
{
  document.getElementById('range-div').innerHTML = '';
  document.getElementById('result').length = 0;
  var row = document.getElementById('range_search');
  if (rangeId <= 0)
  {
    row.style.display = 'none';
  }
  else
  {
    row.style.display = '';
  }
}

function addRange()
{
  var selRange = document.forms['theForm'].elements['act_range'];
  if (selRange.value == 0)
  {
    alert(all_need_not_search);
    return;
  }
  var selResult = document.getElementById('result');
  if (selResult.value == 0)
  {
    alert(pls_search);
    return;
  }
  var id = selResult.options[selResult.selectedIndex].value;
  var name = selResult.options[selResult.selectedIndex].text;

  // 检查是否已经存在
  var exists = false;
  var eles = document.forms['theForm'].elements;
  for (var i = 0; i < eles.length; i++)
  {
    if (eles[i].type=="checkbox" && eles[i].name.substr(0, 13) == 'act_range_ext')
    {
      if (eles[i].value == id)
      {
        exists = true;
        alert(range_exists);
        break;
      }
    }
  }

  // 创建checkbox
  if (!exists)
  {
    var html = '<input name="act_range_ext[]" type="checkbox" value="' + id + '" checked="checked" />' + name + '<br />';
    document.getElementById('range-div').innerHTML += html;
  }
}

/**
 * 搜索赠品
 */
function searchItem1()
{
	if (document.forms['theForm'].elements['act_type'].value == 1){
		alert(price_need_not_search);
		return;
	}
	var filter = new Object;
	filter.keyword  = document.forms['theForm'].elements['keyword1'].value;
	filter.act_range = 3;
	//Ajax.call('favourable.php?is_ajax=1&act=search', filter, searchResponse1, 'GET', 'jsON');  
	var options	=	{
		url:'__URL__/search',
		type:'get',
		dataType:'json',
		data:{'jsON':filter},
		beforeSend:function(){},
		success:function(result){
			searchResponse1(result);
		}
	}
	$.ajax(options);
}

function searchResponse1(result)
{
  if (result.error == '1' && result.message != '')
  {
    alert(result.message);
	return;
  }

  var sel = document.forms['theForm'].elements['result1'];

  sel.length = 0;

  /* 创建 options */
  var goods = result.content;
  if (goods)
  {
    for (i = 0; i < goods.length; i++)
    {
      var opt = document.createElement("OPTION");
      opt.value = goods[i].id;
      opt.text  = goods[i].name;
      sel.options.add(opt);
    }
  }

  return;
}

function addGift()
{
  var selType = document.forms['theForm'].elements['act_type'];
  if (selType.value == 1)
  {
    alert(price_need_not_search);
    return;
  }
  var selResult = document.getElementById('result1');
  if (selResult.value == 0)
  {
    alert(pls_search);
    return;
  }
  var id = selResult.options[selResult.selectedIndex].value;
  var name = selResult.options[selResult.selectedIndex].text;

  // 检查是否已经存在
  var exists = false;
  var eles = document.forms['theForm'].elements;
  for (var i = 0; i < eles.length; i++)
  {
    if (eles[i].type=="checkbox" && eles[i].name.substr(0, 7) == 'gift_id')
    {
      if (eles[i].value == id)
      {
        exists = true;
        alert(range_exists);
        break;
      }
    }
  }

  // 创建checkbox
  if (!exists)
  {
    var table = document.getElementById('gift-table');
    if (table.rows.length == 0)
    {
        var row = table.insertRow(-1);
        var cell = row.insertCell(-1);
        cell.align = 'center';
        cell.innerHTML = '<strong>' + gift + '</strong>';
        var cell = row.insertCell(-1);
        cell.align = 'center';
        cell.innerHTML = '<strong>' + price + '</strong>';
    }
    var row = table.insertRow(-1);
    var cell = row.insertCell(-1);
    cell.innerHTML = '<input name="gift_id[]" type="checkbox" value="' + id + '" checked="checked" />' + name;
    var cell = row.insertCell(-1);
    cell.align = 'right';
    cell.innerHTML = '<input name="gift_price[]" type="text" value="0" size="10" style="text-align:right" />' +
                     '<input name="gift_name[]" type="hidden" value="' + name + '" />';
  }
}

function changeType(typeId)
{
  document.getElementById('gift-div').innerHTML = '<table id="gift-table"></table>';
  document.getElementById('result1').length = 0;
  var row = document.getElementById('type_search');
  if (typeId <= 0)
  {
    row.style.display = '';
  }
  else
  {
    row.style.display = 'none';
  }
}

//-->
</script>



